/*
 * File Name：ActivityManager.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： ActivityManager.java
 * Modify By：res-jxliu
 * Modify Date：2015年3月21日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.utils;

import android.app.Activity;
import android.util.Log;

import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * activity管理类，主要管理当前打开的所有的activity
 * 
 * @author res-jxliu
 * @version ciwong v.1.0 2015年3月21日
 * @since ciwong v.1.0
 */
public class BaseActivityManager
{

    private static final String TAG = "BaseActivityManager";
    /**
     * activity的实例
     */
    private static BaseActivityManager instance;
    private Activity mainActivity;

    public int  getActivityListSize() {
        if(activityList!=null){
            return  activityList.size();
        }
        return 0;
    }

    /**
     * 已经启动的acitivty集合
     */
    private CopyOnWriteArrayList<ActivityInfo> activityList;
    private ArrayList<String> activityListClassName;

    /**
     * 需要结束掉的activity列表
     */
    private List<Activity> needFinishActList;

    /**
     * 刷新最近
     */
    private RefreshLate refreshLater;


    /**
     * 
     * Cnstructor Method。
     * 
     */
    private BaseActivityManager()
    {
        activityList = new CopyOnWriteArrayList<ActivityInfo>();
        activityListClassName = new ArrayList<String>();
        needFinishActList = new ArrayList<Activity>();
    }

    /**
     * 获取activityManager的单例
     * 
     * @return activityManager单例
     */
    public static BaseActivityManager getInstance()
    {
        if (instance == null)
        {
            instance = new BaseActivityManager();
        }
        return instance;
    }

    public Activity getMainActivity()
    {
        return mainActivity;
    }

    public void setMainActivity(Activity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    /**
     * 添加activity
     * @param info
     *            Activity信息
     */
    public void addActivity(ActivityInfo info)
    {
        if (activityList.contains(info))
        {
            activityList.remove(info);
        }
        activityList.add(info);
        activityListClassName.add(info.className);
    }

    /**
     * 
     * 移除Activity
     * 
     * @param info
     *            Activity信息
     */
    public void removeActivity(ActivityInfo info)
    {
        activityListClassName.remove(info.className);
        activityList.remove(info);
    }

    /**
     * 添加需要结束的activity
     * 
     * @param act
     *            activity
     */
    // public void addNeedFinishAct(Activity act)
    // {
    // if (!needFinishActList.contains(act))
    // {
    // needFinishActList.add(act);
    // }
    // }

    /**
     * 
     * 移除需要结束的activity
     * 
     * @param act
     *            activity
     */
    public void removeNeedFinishAct(Activity act)
    {
        needFinishActList.remove(act);
    }

    // /**
    // *
    // * 结束掉activity
    // */
    // public void finishNeedFinishAct()
    // {
    // for (Activity act : needFinishActList)
    // {
    // if (!((act.getParent() != null && act.getParent().getClass()
    // .getName().equals("com.ciwong.xixin.ui.MainActivity")) || act
    // .getClass().getName()
    // .equals("com.ciwong.xixin.ui.MainActivity")))
    // {
    // act.finish();
    // System.out.println("finishAct act=" + act);
    // }
    // }
    // needFinishActList.clear();
    // }

    /**
     * 
     * 结束掉activity
     */
    private void finishNeedFinishAct(List<Activity> list)
    {
        for (Activity act : list)
        {
            act.finish();

        }
    }

    /**
     * 获取activity实例
     * 
     * @param clsName
     *            类名
     * @param tag
     *            activityTag
     * @return 已经启动的activity实例
     */
    public ActivityInfo getActivity(String clsName, String tag)
    {
        int index = activityList.indexOf(new ActivityInfo(clsName, tag));
        if (index >= 0 && index < activityList.size())
        {
            return activityList.get(index);
        }
        return null;
    }

    /**
     * 结束掉所有的activity
     */
    public void killAllActivity()
    {
        if (activityList != null)
        {
            for (Iterator iterator = activityList.iterator(); iterator
                    .hasNext();)
            {
                ActivityInfo activityInfo = (ActivityInfo) iterator.next();
                if (activityInfo != null)
                {
                    Activity activity = activityInfo.getActivity();
                    if (activity != null && !activity.isFinishing())
                    {
                        activity.finish();
                    }
                }
            }
        }
    }

    public RefreshLate getRefreshLater()
    {
        return refreshLater;
    }

    public void setRefreshLater(RefreshLate refreshLater)
    {
        this.refreshLater = refreshLater;
    }

    /**
     * activity信息
     * 
     * @author RES-KUNZHU
     * 
     */
    public static class ActivityInfo
    {
        /**
         * activity的完整类名
         */
        private Activity baseAct;

        /**
         * activity的标签
         */
        private String activityTag;

        /**
         * 类名
         */
        private String className;

        /**
         * 是否需要手势关闭activity标记
         */
        private boolean isNeedActivityAniamtion;

        /**
         * 
         * Cnstructor Method。
         * 
         * @param activity
         *            完整类名
         * @param tag
         *            标签
         */
        public ActivityInfo(BaseActivity activity, String tag)
        {
            this.baseAct = activity;
            this.activityTag = tag;
            this.className = baseAct.getClass().getName();
        }

        /**
         * 
         * Cnstructor Method。
         * 
         * @param clsName
         * @param tag
         */
        private ActivityInfo(String clsName, String tag)
        {
            this.className = clsName;
            this.activityTag = tag;
        }

        /**
         * 设置类名
         * 
         * @param activity
         *            类名
         */
        public void setActivity(BaseActivity activity)
        {
            baseAct = activity;
        }

        /**
         * 设置acitity标签
         * 
         * @param tag
         *            标签
         */
        public void setActivityTag(String tag)
        {
            activityTag = tag;
        }

        /**
         * 获取activity
         * 
         * @return 获取activity
         */
        public Activity getActivity()
        {
            return baseAct;
        }

        /**
         * 获取tag
         * 
         * @return activityTag
         */
        public String getTag()
        {
            return activityTag;
        }

        @Override
        public String toString()
        {
            return className + activityTag;
        }

        @Override
        public boolean equals(Object o)
        {
            if (o instanceof ActivityInfo && toString().equals(o.toString()))
            {
                return true;
            }
            return false;
        }

        public boolean isNeedActivityAniamtion()
        {
            return isNeedActivityAniamtion;
        }

        public void setNeedActivityAniamtion(boolean isNeedActivityAniamtion)
        {
            this.isNeedActivityAniamtion = isNeedActivityAniamtion;
        }

    }

    public Activity getLowerActivity(Activity act)
    {
        int size = activityList.size();
        for (int i = size - 2; i >= 0; i--)
        {
            ActivityInfo info = activityList.get(i);
            if (info.getActivity() == act || !info.isNeedActivityAniamtion)
            {
                continue;
            }
            return info.getActivity();
        }
        return null;
    }

    /**
     * 刷新最近
     * 
     * @author RES-KUNZHU
     * 
     */
    public interface RefreshLate
    {
        /**
         * 
         * 刷新方法
         */
        void doRefresh();
    }

    // public Activity getActivityByName(String activityName, String tag)
    // {
    // if (activityName == null || "".equals(activityName))
    // {
    // return null;
    // }
    // for (ActivityInfo info : activityList)
    // {
    // if (info.toString().contains(activityName + tag))
    // {
    // return info.getActivity();
    // }
    // }
    // return null;
    // }

    /**
     * 
     * 判断该tag的activity是否已经在栈中
     * 
     * @return
     */
    public boolean isActivityExist(String activityName, String tag)
    {
        for (ActivityInfo info : activityList)
        {
            if (info.toString().equals(activityName + tag))
            {
                return true;
            }
        }
        return false;
    }


    /**
     * 
     * 清理除了当前activity和主界面activity以外的所有activity
     * 
     * @param activity
     *            activity
     */
    public void clearAllActivityWithOutThis(Activity activity)
    {
        List<Activity> tempList = new ArrayList<Activity>();
        for (ActivityInfo info : activityList)
        {
            if (info.getActivity() != activity)
            {
                tempList.add(info.getActivity());
            }
        }
        finishNeedFinishAct(tempList);
    }

    public String getTopClassNameInApp(){
        if(activityList!=null&&activityList.size()>0){
            return activityList.get(activityList.size()-1).className;
        }
        return "";
    }

    public  boolean isContainsActivityInStack(Class calzz){
        if(activityListClassName!=null&&calzz!=null&&activityListClassName.contains(calzz.getName())){
            return true;
        }
        return  false;

    }

}
